//
//  UIButton+Block.m
//  fmapp
//
//  Created by zhouyang on 14-8-29.
//  Copyright (c) 2014年 CheDong Technology Co., Ltd. All rights reserved.
//

#import "UIButton+Block.h"

@implementation UIButton (Block)

static char overviewKey;
@dynamic event;

- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block
{
    objc_setAssociatedObject(self, &overviewKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}

- (void) handleControlEvent:(UIControlEvents)controlEvent withSelectBlock:(SelectActionBlock)action
{
     objc_setAssociatedObject(self, &overviewKey, action, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(SelectCallActionBlock:) forControlEvents:controlEvent];
}

- (void)callActionBlock:(id)sender
{
    ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &overviewKey);
    if (block)
    {
        block();
    }
}

- (void)SelectCallActionBlock:(id)sender
{
    UIButton* button=(UIButton*)sender;
    SelectActionBlock block = (SelectActionBlock)objc_getAssociatedObject(self, &overviewKey);
    if (block)
    {
        block(button.selected);
    }

}

@end
